package com.ironman.compute;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ironman.common.OriginalSimpleData;
import com.ironman.common.structure.FixedSizeLinkedBlockingDeque;
import com.ironman.event.topic.dami.OriginalTopic;
import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * 风矢量计算
 */
@Slf4j
public class GpsEmptyComputer {

    private final FixedSizeLinkedBlockingDeque<String> gpsFixedSizeLinkedBlockingDeque = new FixedSizeLinkedBlockingDeque<>(300);

    public void init(Consumer<List<String>> gpsConsumer) {
        Dami.<OriginalSimpleData, Void>bus().listen(OriginalTopic.ORIGINAL_TOPIC + "gps", payload -> {
            OriginalSimpleData originalSimpleData = payload.getContent();
            Date ts = originalSimpleData.getTs();
            String data = originalSimpleData.getData();
            String[] split = data.split(",");
            String currentGps = StrUtil.format("{},{},{}",
                    split[1],
                    split[2],
                    DateUtil.format(ts, "HHmmss")
            );
            gpsFixedSizeLinkedBlockingDeque.offerFirst(currentGps);

            List<String> recentGps = gpsFixedSizeLinkedBlockingDeque.all();
            recentGps = CollectionUtil.isEmpty(recentGps) ? new ArrayList<>() : recentGps;

            gpsConsumer.accept(recentGps);
        });
    }
}
